import 'dart:async';

import 'package:auto_route/auto_route.dart';
import 'package:flutter/foundation.dart';

import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';

import '../../config/route/bt_app_router.dart';
import '../../config/route/bt_app_router.gr.dart';
import '../../utils/bt_tool_utils.dart';

@RoutePage(name: "guideRoute")
class BtGuidePage extends StatefulWidget {
  const BtGuidePage({super.key});

  @override
  State<BtGuidePage> createState() => _BtGuideState();
}

class _BtGuideState extends State<BtGuidePage> {
  final PageController _pageController = PageController();

  int _currentPage = 0;

  Timer? _timer;

  @override
  void initState() {
    super.initState();
    // 开始计时
    _startTimer();
  }

  // 开始计时的方法
  void _startTimer() {
    _timer = Timer.periodic(const Duration(seconds: 2), (timer) {
      // 自动跳转到下一页
      if (_currentPage < 2) {
        _pageController.nextPage(duration: const Duration(milliseconds: 500), curve: Curves.ease);
      } else {
        // 如果已经是最后一页，取消定时器并跳转到登录页
        _timer?.cancel();
        setGuideShowed(true);
        GetIt.instance<BtAppRouter>().replaceAll([const BtLoginRoute()]);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return NotificationListener<ScrollNotification>(
      onNotification: (ScrollNotification notification) {
        if (notification is ScrollUpdateNotification) {
          // 重置定时器
          _timer?.cancel();
          _startTimer();
          // 获取当前滚动位置
          double currentPosition = notification.metrics.pixels;
          // 获取最大可滚动位置
          double maxScrollExtent = notification.metrics.maxScrollExtent;

          // 如果当前位置在最后一页，并且用户继续往左滚动
          if (currentPosition >= maxScrollExtent && (notification.dragDetails?.primaryDelta ?? 0) < 0) {
            setGuideShowed(true);
            GetIt.instance<BtAppRouter>().replaceAll([const BtLoginRoute()]);
          }
        }
        return false;
      },
      child: PageView(
        controller: _pageController,
        onPageChanged: (int page) {
          setState(() {
            _currentPage = page;
          });
          if (kDebugMode) {
            print('Scroll to $_currentPage');
          }
        },
        children: [
          SizedBox(
            width: double.infinity,
            height: double.infinity,
            child: Image.asset(
              'assets/images/bt_guide_pg_one.png',
              fit: BoxFit.fill,
            ),
          ),
          SizedBox(
            width: double.infinity,
            height: double.infinity,
            child: Image.asset(
              'assets/images/bt_guide_pg_two.png',
              fit: BoxFit.fill,
            ),
          ),
          SizedBox(
            width: double.infinity,
            height: double.infinity,
            child: Image.asset(
              'assets/images/bt_guide_pg_three.png',
              fit: BoxFit.fill,
            ),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _timer?.cancel();
    _pageController.dispose();
    super.dispose();
  }
}
